//
//  MyDocument.m
//  Sofia
//
//  Created by Anton Chebotaev on 19/11/08.
//  Copyright IFMO 2008 . All rights reserved.
//

#import "SFEntryController.h"


@implementation SFEntryController

- (id)init
{
    self = [super init];
    if (self) {
    
        // Add your subclass-specific initialization here.
        // If an error occurs here, send a [self release] message and return nil.
    
    }
    return self;
	
	//webModeView = [WebView initByReferencingURL:<#(NSURL *)url#>
}

- (NSString *)windowNibName
{
    // Override returning the nib file name of the document
    // If you need to use a subclass of NSWindowController or if your document supports multiple NSWindowControllers, you should remove this method and override -makeWindowControllers instead.
    return @"MyDocument";
}

- (void)windowControllerDidLoadNib:(NSWindowController *) aController
{
    [super windowControllerDidLoadNib:aController];
    
	[self changeView:nil];
}

- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError
{
    // Insert code here to write your document to data of the specified type. If the given outError != NULL, ensure that you set *outError when returning nil.

    // You can also choose to override -fileWrapperOfType:error:, -writeToURL:ofType:error:, or -writeToURL:ofType:forSaveOperation:originalContentsURL:error: instead.

    // For applications targeted for Panther or earlier systems, you should use the deprecated API -dataRepresentationOfType:. In this case you can also choose to override -fileWrapperRepresentationOfType: or -writeToFile:ofType: instead.

    if ( outError != NULL ) {
		*outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:NULL];
	}
	return nil;
}

- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError
{
    // Insert code here to read your document from the given data of the specified type.  If the given outError != NULL, ensure that you set *outError when returning NO.

    // You can also choose to override -readFromFileWrapper:ofType:error: or -readFromURL:ofType:error: instead. 
    
    // For applications targeted for Panther or earlier systems, you should use the deprecated API -loadDataRepresentation:ofType. In this case you can also choose to override -readFromFile:ofType: or -loadFileWrapperRepresentation:ofType: instead.
    
    if ( outError != NULL ) {
		*outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:NULL];
	}
    return YES;
}

- (IBAction)testAction:(id)sender
{
	// Load user aacounts
	NSData *userAccountsAsData = [[NSUserDefaults standardUserDefaults] objectForKey:SFAAccouns];
    NSArray *accounts = [NSMutableArray arrayWithArray:[NSKeyedUnarchiver unarchiveObjectWithData:userAccountsAsData]];	
	
	// Load default number
	int defaultNumber = [[NSUserDefaults standardUserDefaults] integerForKey:SFADefaultAccountNumber];
	
	

	LJAccount *account = [accounts objectAtIndex:defaultNumber];
	if(account)
	{
		if(![account isLoggedIn])
		{
			[account loginWithPassword:@"******"];// flags:LJGetMenuLoginFlag];
			if ([account loginMessage]) {
				NSRunAlertPanel(@"Message from the Server", // *title,
								[account loginMessage], // *msg,
								nil, // *defaultButton,
								nil, // *alternateButton,
								nil); // *otherButton, ...);
			}
			else
			{
				NSRunAlertPanel(@"Congratulations", // *title,
								@"access to antonche's livejournal granted", // *msg,
								@"Cheers!", // *defaultButton,
								nil, // *alternateButton,
								nil); // *otherButton, ...);
				
			}
			[postToMenu setMenu:[account journalMenu]];
			[userpicturesMenu setMenu:[account userPicturesMenu]];
		}
		NSString *defaultUserPictureURL = [[account defaultUserPictureURL] absoluteString];
		NSLog(defaultUserPictureURL);
		NSLog(@"testAction passed");
	}
	NSLog(@"BIG action tested.");
}

- (IBAction)changeView:(id)sender
{
	int selection = [modeButton selectedSegment];
	NSView *newView;
	switch (selection) {
		case 0:
			newView = liveModeView;
			break;
		case 1:
			newView = rawModeView;
			break;
		default:
			break;
	}
	[modeView addSubview:newView];
    [newView setFrame:[modeView bounds]];	
	NSLog(@"Changing subView to %d", selection);
}

- (IBAction)getMusic:(id)sender
{
	NSString *music = [[SFBasicPerformer sharedBasicPerformer] getFormatedMusicString];
	[currentMusic setStringValue:music];
}
@end
